package com.regentsoft.cloudback.operateAdmin.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.regentsoft.cloudback.base.Pager;
import com.regentsoft.cloudback.operateAdmin.dao.ActivationMapper;
import com.regentsoft.cloudback.operateAdmin.entity.Activation;
import com.regentsoft.cloudback.operateAdmin.entity.CompanyName;
import com.regentsoft.cloudback.operateAdmin.service.ActivationService;
import com.regentsoft.cloudback.operateAdmin.service.ActivationSubCodeService;
import com.regentsoft.cloudback.vo.QueryActivationVo;

/**
 * 
 * @author czd
 *
 * @date 2017年11月29日
 *
 * @version 1.0
 */
@Service
public class ActivationServiceImpl  implements ActivationService {
	@Autowired
	private ActivationMapper mapper;
	@Autowired
	private ActivationSubCodeService activationSubCodeService;
	@Override
	public Activation findById(String pk) throws Exception {
		return mapper.findById(pk);
	}

	@Override
	public List<Activation> findList(Activation obj) throws Exception {
		return mapper.findList(obj);
	}

	@Override
	public Pager findPageList(Activation obj, Integer pageNumber, Integer pageSize) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Pager findPageList(Integer pageNumber, Integer pageSize) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		int total = getTotalCount(params);
		pageSize = pageSize == null || pageSize <= 0 ? 10 : pageSize;
		int totalPage = (total + pageSize - 1) / pageSize;
		pageNumber = pageNumber == null || pageNumber < 1 ? 1 : pageNumber > totalPage ? totalPage : pageNumber;
		Pager pager = new Pager(pageNumber, pageSize, total);
		if (total > 0) {
		    params.put("offset", (pager.getPageNumber() - 1) * pageSize);
		    params.put("pageSize", pageSize);
		    List<QueryActivationVo> list = this.queryByPageCode(params);
		    pager.setDatas(list);
		}
		return pager;
	}

	@Override
	public int add(Activation obj) throws Exception {
		return mapper.add(obj);
	}

	@Override
	public int update(Activation obj) throws Exception {
		return mapper.update(obj);
	}

	@Override
	public int delete(String pk) throws Exception {
		return mapper.deleteById(pk);
	}

	@Override
	public int getTotalCount(Map<String, Object> map) throws Exception {
		return mapper.getTotalCount(map);
	}

	@Override
	public List<Activation> queryByPage(Map<String, Object> map) throws Exception {
		return mapper.queryByPage(map);
	}

	@Override
	public boolean findByCode(String randomCode) {
		return mapper.findByCode(randomCode)==1;
	}

	@Override
	@Transactional
	public void addActivation(String codeId,String code, String companyId, Integer capacity, String note) throws Exception {
		Activation activation = new Activation();
		try {
			if(StringUtils.isEmpty(codeId)) {
				activation.setCtId(UUID.randomUUID().toString());
				activation.setCapacity(capacity);
				activation.setCode(code);
				activation.setCompanyId(companyId);
				activation.setCreateTime(new Date());
				if(note!=null) {
					activation.setNote(note);
				}
				mapper.add(activation);

			}else {
				
			}
			//统计子激活码数量，并返回最大的
			int from = activationSubCodeService.getTotalCount(code);
			capacity =capacity+from;
			//生成激活码（多条数据）
			activationSubCodeService.generatorSubCode(companyId, from,capacity, code, null);
			if(codeId!=null) {
				//修改激活码容量
				activation.setCtId(codeId);
				activation.setCapacity(capacity);
				mapper.update(activation);
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(),e);
		}

	}

	@Override
	public List<CompanyName> queryCompanyId(String companyId) {
		return mapper.queryCompanyId("%"+companyId+"%");
	}

	@Override
	public List<QueryActivationVo> queryByPageCode(Map<String, Object> map) throws Exception {
		return mapper.queryByPageCode(map);
	}

	@Override
	@Transactional
	public boolean deleteActivation(String code) throws Exception {
		//判断激活码是否使用
		boolean flag = mapper.countByCode(code)>0;
		if(flag) {
			return false;
		}
		try {
			mapper.deleteActivation(code);
			mapper.deleteActivationCode(code);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(),e);
		}
		return true;
		
	}

	@Override
	public QueryActivationVo queryActivationDetail(String code) {
		return mapper.queryActivationDetail(code);
	}

	
}
